﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rose.Models;
using Rose.Helper;

namespace Rose.Helper
{
    public class FocusRelationHelpModel
    {

        private  Database_RoseEntities db = new Database_RoseEntities();

        #region Update focus count
        private  void  UpdateFocusCount(string userId, string focusId, bool isAdd)
        {

            UserInfo selfInfo = db.UserInfos.Where(c => c.Id == userId).First();
            UserInfo focusInfo = db.UserInfos.Where(c => c.Id == focusId).First();
            if (selfInfo == null || focusInfo == null)
                return;
            if (isAdd)
            {
                selfInfo.FocusCount += 1;
                focusInfo.BeFocusCount += 1;
                focusInfo.Score += int.Parse(Rose.ViewRes.Defaultresx.BeFocusAddScore);
            }
            else
            {
                selfInfo.FocusCount -= 1;
                focusInfo.BeFocusCount -= 1;
                focusInfo.Score -= int.Parse(Rose.ViewRes.Defaultresx.BeFocusAddScore);
            }

            if (selfInfo.FocusCount < 0)
                selfInfo.FocusCount = 0;
            if (focusInfo.BeFocusCount < 0)
                focusInfo.BeFocusCount = 0;

            db.SaveChanges();

            return;
            //这是单方面的增加
        }
        #endregion

        #region  RemoveFoucsFromRelation

        public  bool RemoveFoucsFromRelation(string selfId, string focusId)
        {
            try
            {
                if (IsRelationExist(selfId, focusId))
                {
                    FocusRelation focusRelation = db.FocusRelations.Where(c => c.UserId == selfId && c.FocusId == focusId).First();
                    db.FocusRelations.DeleteObject(focusRelation);
                    db.SaveChanges();
                    UpdateFocusCount(selfId, focusId, false);
                }
                return true;
            }
            catch
            {
                return false;

            }
        }
        #endregion


        #region helpcheck IsExist

        public  bool IsRelationExist(string selfId, string focusId)
        {
            var returnModel = db.FocusRelations.Where(c => c.UserId == selfId && c.FocusId == focusId);
            return returnModel.Count() > 0 ? true : false;
        }

        #endregion 

        #region addFocus
        public  bool AddFocusToRelation(string selfId, string focusId)
        {
            try
            {

                if (!IsRelationExist(selfId, focusId))
                {
                    FocusRelation focusRelation = new FocusRelation()
                    {
                        CreateTime = DateTime.Now,
                        Id = Guid.NewGuid(),
                        FocusId = focusId,
                        UserId = selfId

                    };
                    AddFocusToRelation(focusRelation);

                    UpdateFocusCount(selfId, focusId, true);
                }

                return true;
            }
            catch
            { return false; }
        }

        private  bool AddFocusToRelation(FocusRelation model)
        {
            try
            {
                db.AddToFocusRelations(model);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region  GetFocusList

        private  List<UserInfo> GetFocusUserInfoList(string selfId)
        {
            
            List<UserInfo> returnModel = new List<UserInfo>();
            
            List<FocusRelation> relations = db.FocusRelations.Where(c => c.UserId == selfId).ToList();
            foreach (var relation in relations)
            {

                try
                {
                    UserInfo userInfo =new CheckHelpModel().CheckUserInfo(relation.FocusId);
                    returnModel.Add(userInfo);
                }

                catch
                {
                    continue;
                }

            }
            return returnModel;
        }

        public  List<RecommendModel> GetFriendAbstractList(string selfId, int currentpage, int pagesize)
        {
            List<RecommendModel> returnModel = new List<RecommendModel>();
            var recommendList = GetFocusUserInfoList(selfId);
           

            foreach (UserInfo recommend in recommendList)
            {
                RecommendModel addModel = new RecommendModel()
                {
                    UserId = recommend.Id,
                    TrueName = recommend.Name ?? string.Empty,
                    ImagePath = recommend.ImagePath ?? string.Empty,
                    Address = recommend.Address ?? string.Empty,
                    University=recommend.University??string.Empty,
                    MajorClass = recommend.CollegeMajorClass ?? string.Empty,
                    SchoolYear = recommend.SchoolYear.ToString(),
                    Hobbies = new UserInfoHelpModel().GetHobbies(recommend.Id) ?? string.Empty,
                    Gender=recommend.Gender,
                    College=recommend.College
                };
                returnModel.Add(addModel);
            }
            return returnModel;
        }

        public  int GetFriendsCount(string selfId)
        {
            return 
            GetFocusUserInfoList(selfId) == null ? 0 : GetFocusUserInfoList(selfId).Count();
        }

        #endregion
    }


}